package spzx.exception;

import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 捕获ArithmeticException异常，并返回Result对象
     * @param se
     * @return
     */
    @ExceptionHandler(value = SpzxException.class)
    public Result handleSpzxException(SpzxException se) {
        se.printStackTrace();  // 控制台打印异常日志
        return Result.build(null,se.getCode(),se.getMessage());
    }


    /**
     * 捕获NullPointerException异常，并返回Result对象
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    public Result handleNullPointerException(NullPointerException e) {
        e.printStackTrace();  // 控制台打印异常日志
        return Result.build(null, ResultCodeEnum.NULLPOINTER);
    }

    /**
     * 捕获Exception异常，并返回Result对象
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();  // 控制台打印异常日志
        return Result.build(null, ResultCodeEnum.FAILURE);
    }
}
